﻿using EMoney.IStock.Client.Business.QQ;
using EMoney.IStock.Client.Framework;
using EMoney.IStock.Client.Models.QQ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.IO;
using EMoney.IStock.Client.Business;
using System.Collections.Specialized;
using EMoney.IStock.Client.Business.MC;
using EMoney.IStock.Client.Models.MC;
using System.Linq.Dynamic;

namespace EMoney.IStock.Client.Web.Controllers
{
    public class QQController : Controller
    {
        //
        // GET: /QQ/
        #region 我的大赛
        public ActionResult Index()
        {
            //ViewBag.Apply = "1";
            //ViewBag.QQ = "115536016";
            //return View();
            //new TradeBll().GetAssets("115536016");
            //new TradeBll().GetUinByOpenId("80168852BCF3966451F02675DB6E1413", "");

            int userid = 0;
            string color = Request["color"];
            string token = Request["token"];
            string associatedlogin = Request["associatedlogin"];
            
            if (!string.IsNullOrEmpty(token))    //token有值时，执行认证操作
            {
                userid = TokenBll.GetUserIDByToken(token);
            }
            else
            {
                userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
            }

            string qq = string.Empty;

            if (userid == 0)
            {
                return RedirectToAction("bm", new { color = color, associatedlogin = associatedlogin });
            }
            else
            {
                qq = TokenBll.GetQQByUserID(userid);

                if (string.IsNullOrEmpty(qq))
                {
                    return RedirectToAction("bm", new { color = color, associatedlogin = associatedlogin });
                }
            }

            TradeBll tradeBll = new TradeBll();

            bool isPlayer = tradeBll.IsPlayer(qq);

            ViewBag.QQ = qq;

            if (!isPlayer)  //未参赛
            {
                return RedirectToAction("bm", new { color = color, associatedlogin = associatedlogin });
            }
            else
            {
                if (!tradeBll.IsLottery(qq))    //未抽奖
                {
                    return RedirectToAction("lottery", new { color = color });
                }
                else
                {
                    string css = StringHelper.GetStaticFile("IStockClientStatic/qq/css/white.css");
                    string scrollcss = StringHelper.GetStaticFile("IStockClientStatic/css/scroll.css");

                    if (!string.IsNullOrEmpty(color) && color == "black")
                    {
                        css = StringHelper.GetStaticFile("IStockClientStatic/qq/css/black.css");
                        scrollcss = StringHelper.GetStaticFile("IStockClientStatic/css/scrollblock.css");

                        ViewBag.Color = "black";
                    }
                    else
                    {
                        ViewBag.Color = "default";
                    }

                    ViewBag.PageCss = css;
                    ViewBag.ScrollCss = scrollcss;
                    ViewBag.Assets = tradeBll.GetAssets(qq);

                    DateTime dtNow = DateTime.Now;

                    ViewBag.Start = StringHelper.FormatDateTime(dtNow.AddMonths(-1));
                    ViewBag.End = StringHelper.FormatDateTime(dtNow);
                }
            }

            return View();
        }

        /// <summary>
        /// 异步获取用户资产信息
        /// </summary>
        /// <returns></returns>
        public string assets()
        {
            string ret = string.Empty;
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid > 0)
            {
                string qq = TokenBll.GetQQByUserID(userid);

                if (!string.IsNullOrEmpty(qq))
                {
                    TradeBll tradeBll = new TradeBll();

                    Assets assets = tradeBll.GetAssets(qq);

                    if (assets != null)
                    {
                        ret = JsonHelper.ToJson(assets);
                    }
                }
            }

            return ret;
        }

        /// <summary>
        /// 持仓
        /// </summary>
        /// <returns></returns>
        public ActionResult stocks()
        {
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid > 0)
            {
                string qq = TokenBll.GetQQByUserID(userid);

                if (!string.IsNullOrEmpty(qq))
                {
                    TradeBll tradeBll = new TradeBll();

                    ViewBag.Stocks = tradeBll.GetStocks(qq);
                }
            }

            return View();
        }

        /// <summary>
        /// 待撤单
        /// </summary>
        /// <returns></returns>
        public ActionResult cancelorder()
        {
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid > 0)
            {
                string qq = TokenBll.GetQQByUserID(userid);

                if (!string.IsNullOrEmpty(qq))
                {
                    TradeBll tradeBll = new TradeBll();

                    ViewBag.UnCancelOrder = tradeBll.GetUnCancelOrder(qq);
                }
            }

            return View();
        }

        /// <summary>
        /// 撤单
        /// </summary>
        /// <returns></returns>
        public string docancelorder()
        {
            string ret = string.Empty;
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid > 0)
            {
                string qq = TokenBll.GetQQByUserID(userid);

                if (!string.IsNullOrEmpty(qq))
                {
                    string oid = Request["oid"];
                    string code = Request["code"];

                    TradeBll tradeBll = new TradeBll();

                    ret = tradeBll.DoCancelOrder(qq, oid, code);
                }
            }

            return ret;
        }

        /// <summary>
        /// 成交查询
        /// </summary>
        /// <returns></returns>
        public ActionResult dealorders()
        {
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid > 0)
            {
                string qq = TokenBll.GetQQByUserID(userid);

                if (!string.IsNullOrEmpty(qq))
                {
                    TradeBll tradeBll = new TradeBll();

                    string start = Request["start"];
                    string end = Request["end"];

                    if (string.IsNullOrEmpty(start) || string.IsNullOrEmpty(end))
                    {
                        DateTime dtNow = DateTime.Now;
                        DateTime dtTradeDate = DateTime.MinValue;

                        if (TradeDateBll.IsTradeDate(dtNow.Date))
                        {
                            dtTradeDate = dtNow.Date;
                        }
                        else
                        {
                            dtTradeDate = TradeDateBll.GetPreTradeDate(dtNow);
                        }

                        start = StringHelper.FormatDateTime(dtTradeDate);
                        end = StringHelper.FormatDateTime(dtTradeDate);
                    }

                    ViewBag.DealOrders = tradeBll.GetDealOrders(qq, start, end);
                }
            }

            return View();
        }        

        /// <summary>
        /// 委托
        /// </summary>
        /// <returns></returns>
        public string addorder()
        {
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid == 0)
            {
                return string.Empty;
            }
            else
            {
                string qq = TokenBll.GetQQByUserID(userid);

                if (string.IsNullOrEmpty(qq))
                {
                    return string.Empty;
                }
                else
                {
                    decimal price = 0;
                    int count = 0;

                    string code = Request["code"];
                    string buysell = Request["bs"];
                    string reqPrice = Request["p"];
                    string reqCount = Request["c"];

                    if (string.IsNullOrEmpty(code) || string.IsNullOrEmpty(buysell) || string.IsNullOrEmpty(reqPrice)
                        || string.IsNullOrEmpty(reqCount))
                    {
                        return string.Empty;
                    }

                    string codePrefix = code.Substring(0, 1);

                    switch (codePrefix)
                    {
                        case "6":
                            code = "sh" + code;
                            break;
                        case "0":
                        case "3":
                            code = "sz" + code;
                            break;
                        default:
                            return string.Empty;
                    }

                    if (!decimal.TryParse(reqPrice, out price))
                    {
                        return string.Empty;
                    }

                    if (!int.TryParse(reqCount, out count))
                    {
                        return string.Empty;
                    }

                    if (count % 100 > 0)
                    {
                        return string.Empty;
                    }

                    TradeBll tradeBll = new TradeBll();

                    return tradeBll.AddOrder(qq, code, buysell, price, count);
                }
            }
        }

        public string quotes()
        {
            string code = Request["code"];

            TradeBll tradeBll = new TradeBll();

            return tradeBll.GetQuotesData(code);
        }

        public string veryfy()
        {
            //uid、emcode
            return "{\"result\": true}";
        }

        /// <summary>
        /// 报名页面
        /// </summary>
        /// <returns></returns>
        public ActionResult bm()
        {
            string qq = string.Empty;
            string color = Request["color"];
            string associatedlogin = Request["associatedlogin"];    //1是联合登录 0则不是
            string css = StringHelper.GetStaticFile("IStockClientStatic/qq/css/white.css");

            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid > 0)
            {
                qq = TokenBll.GetQQByUserID(userid);

                if (!string.IsNullOrEmpty(qq))
                {
                    TradeBll tradeBll = new TradeBll();

                    bool isPlayer = tradeBll.IsPlayer(qq);

                    if (isPlayer)
                    {
                        if (!tradeBll.IsLottery(qq))    //未抽奖
                        {
                            return RedirectToAction("lottery", new { color = color });
                        }
                        else
                        {
                            return RedirectToAction("index", new { color = color });    //确认已参赛的用户显示资产信息
                        }
                    }
                }
            }

            ViewBag.UserID = userid;
            ViewBag.QQ = qq;

            if (!string.IsNullOrEmpty(color) && color == "black")
            {
                css = StringHelper.GetStaticFile("IStockClientStatic/qq/css/black.css");

                ViewBag.Color = "black";
            }
            else
            {
                ViewBag.Color = "default";
            }

            ViewBag.PageCss = css;
            ViewBag.Associatedlogin = associatedlogin;

            return View();
        }

        /// <summary>
        /// 抽奖
        /// </summary>
        /// <returns></returns>
        public ActionResult lottery()
        {
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid > 0)
            {
                string qq = TokenBll.GetQQByUserID(userid);

                if (!string.IsNullOrEmpty(qq))
                {
                    string color = Request["color"];
                    string css = StringHelper.GetStaticFile("IStockClientStatic/qq/css/white.css");

                    if (!string.IsNullOrEmpty(color) && color == "black")
                    {
                        css = StringHelper.GetStaticFile("IStockClientStatic/qq/css/black.css");

                        ViewBag.Color = "black";
                    }
                    else
                    {
                        ViewBag.Color = "default";
                    }

                    ViewBag.PageCss = css;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

            return View();
        }

        /// <summary>
        /// 绑定QQ
        /// </summary>
        /// <returns></returns>
        public ActionResult bind()
        {
            string ret = string.Empty;
            string p = Request["p"];
            string gid = Request["gid"];
            string openid = Request["openid"];
            string color = string.Empty;

            if (string.IsNullOrEmpty(p) || string.IsNullOrEmpty(gid) || string.IsNullOrEmpty(openid))
            {
                ret = "非法绑定";
            }
            else
            {
                p = p.Replace(" ", "+");

                p = EncryptionUtil.Decrypt(p);

                if (string.IsNullOrEmpty(p))
                {
                    return Content("解密失败");
                }
                else
                {
                    string[] arrPara = p.Split('^');

                    if (arrPara != null && arrPara.Length >= 3)
                    {
                        TradeBll tradeBll = new TradeBll();

                        ret = tradeBll.BindQQ(arrPara[0], arrPara[1], arrPara[2], gid, openid);

                        if (arrPara.Length == 4)
                        {
                            color = arrPara[4];

                            string css = StringHelper.GetStaticFile("IStockClientStatic/qq/css/white.css");

                            if (!string.IsNullOrEmpty(color) && color == "black")
                            {
                                css = StringHelper.GetStaticFile("IStockClientStatic/qq/css/black.css");
                            }

                            ViewBag.PageCss = css;
                        }
                    }
                    else
                    {
                        return Content("参数非法");
                    }
                }
            }

            ViewBag.Ret = ret;
            ViewBag.Color = color;

            return View();
        }

        /// <summary>
        /// 获取报名验证码
        /// </summary>
        /// <returns></returns>
        public string code()
        {
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid == 0)
            {
                return string.Empty;
            }
            else
            {
                string qq = TokenBll.GetQQByUserID(userid);

                if (string.IsNullOrEmpty(qq))
                {
                    return string.Empty;
                }
                else
                {
                    string mobile = Request["m"];

                    TradeBll tradeBll = new Business.QQ.TradeBll();

                    return tradeBll.GetCode(qq, mobile);
                }
            }
        }

        /// <summary>
        /// 报名参赛
        /// </summary>
        /// <returns></returns>
        public string reg()
        {
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid == 0)
            {
                return string.Empty;
            }
            else
            {
                string qq = TokenBll.GetQQByUserID(userid);

                if (string.IsNullOrEmpty(qq))
                {
                    return string.Empty;
                }
                else
                {
                    TradeBll tradeBll = new TradeBll();

                    string code = Request["code"];
                    string tel = Request["tel"];

                    return tradeBll.RegUser(qq, code, tel);
                }
            }
        }

        /// <summary>
        /// 抽奖
        /// </summary>
        /// <returns></returns>
        public string lty()
        { 
            string ret = string.Empty;
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid > 0)
            {
                string qq = TokenBll.GetQQByUserID(userid);

                if(!string.IsNullOrEmpty(qq))
                {
                    TradeBll tradeBll = new TradeBll();

                    bool result = tradeBll.Lottery(qq);
                    
                    if(result)
                    {
                        ret = "1";
                    }
                    else
                    {
                        ret = "0";
                    }
                }
            }

            return ret;
        }

        /// <summary>
        /// 放弃抽奖
        /// </summary>
        /// <returns></returns>
        public void abandon()
        { 
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号

            if (userid > 0)
            {
                string qq = TokenBll.GetQQByUserID(userid);

                if (!string.IsNullOrEmpty(qq))
                {
                    TradeBll tradeBll = new TradeBll();

                    tradeBll.AbandonLottery(qq);
                }
            }
        }
        #endregion

        #region 高手数据
        /// <summary>
        /// 总排名
        /// </summary>
        /// <returns></returns>
        public ActionResult rank()
        {            

            RankBll rank_bll = new RankBll();
            int userid = 0;
            if (!string.IsNullOrEmpty(Request["token"]))    //token有值时，执行认证操作
            {
                userid = TokenBll.GetUserIDByToken(Request["token"]);
            }
            else
            {
                userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
            }
            ViewBag.UserId = userid;
            LogManager.DefaultLogger.Debug("[userid=" + userid + "]");
            LogManager.DefaultLogger.Debug("[token=" + Request["token"] + "]");
            string qq = string.Empty;
            qq = TokenBll.GetQQByUserID(userid);
            if (!string.IsNullOrEmpty(qq))
            {
                //订阅列表
                ViewBag.EmRss = rank_bll.GetEmRssList(qq);
            }

            //换肤
            string css = StringHelper.GetStaticFile("IStockClientStatic/qq/css/white.css");//white
            string scrollcss = StringHelper.GetStaticFile("IStockClientStatic/css/scroll.css");
            if (!string.IsNullOrEmpty(Request["color"]) && Request["color"] == "black")
            {
                css = StringHelper.GetStaticFile("IStockClientStatic/qq/css/black.css");
                scrollcss = StringHelper.GetStaticFile("IStockClientStatic/css/scrollblock.css");
                ViewBag.Color = "black";
            }
            else
            {
                ViewBag.Color = "default";
            }
            ViewBag.ScrollCss = scrollcss;
            ViewBag.PageCss = css;
            //获取更新时间
            List<Rank> lst = rank_bll.GetRankLst();
            if (lst != null && lst.Count > 0)
            {
                Rank rank = lst.Take(1).FirstOrDefault();
                ViewBag.UpTime = rank.c_time;
            }
            //高手聚焦板块
            FocusBll fbll = new FocusBll();
            DateTime lastDate = DateTime.Now.Date;
            List<TB_FocusBlock> block_lst = fbll.GetFocusBlockList(lastDate.AddDays(-1), lastDate.AddDays(1));
            if (block_lst != null)
            {
                block_lst = block_lst.OrderByDescending(x => x.TradeDate).ThenByDescending(x => x.Number).ThenByDescending(x => x.TolFund).Take(3).ToList();
            }
            ViewBag.FocusBlock = block_lst;

            return View();
        }

        /// <summary>
        /// 总排名（大屏）
        /// </summary>
        /// <returns></returns>
        public ActionResult rank_dp(string type = "daping")
        {
            RankBll rank_bll = new RankBll();
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
            string qq = string.Empty;
            qq = TokenBll.GetQQByUserID(userid);
            if (!string.IsNullOrEmpty(qq))
            {
                //订阅列表
                ViewBag.EmRss = rank_bll.GetEmRssList(qq);
            }            
            List<Rank> ranklst = rank_bll.GetRankLst();

            string sortFlag = Request["SortFlag"];
            string sortName = Request["SortName"];
            if (!string.IsNullOrEmpty(sortName))
            {
                var query = ranklst.AsQueryable();
                ranklst = query.OrderBy(sortName + " " + sortFlag).ToList();
            }
            ViewBag.RankLst = ranklst;
            ViewBag.SortFlag = sortFlag;
            ViewBag.SortName = sortName;
            ViewBag.RankType = type;
            return View();
        }


        /// <summary>
        /// 周收益排行(大屏)
        /// </summary>
        /// <returns></returns>
        public ActionResult rankweek_dp(string type = "daping")
        {
            RankBll rank_bll = new RankBll();
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
            string qq = string.Empty;

            qq = TokenBll.GetQQByUserID(userid);
            if (!string.IsNullOrEmpty(qq))
            {
                //订阅列表
                ViewBag.EmRss = rank_bll.GetEmRssList(qq);
            }

            List<Rank> ranklst = rank_bll.GetRankWeekLst();
            string sortFlag = Request["SortFlag"];
            string sortName = Request["SortName"];
            if (!string.IsNullOrEmpty(sortName))
            {
                var query = ranklst.AsQueryable();
                ranklst = query.OrderBy(sortName + " " + sortFlag).ToList();
            }
            ViewBag.RankWeekLst = ranklst;
            ViewBag.SortFlag = sortFlag;
            ViewBag.SortName = sortName;
            ViewBag.RankType = type;
            return View();
        }


        /// <summary>
        /// 昨日收益排行(大屏)
        /// </summary>
        /// <returns></returns>
        public ActionResult rankday_dp(string type = "daping")
        {
            RankBll rank_bll = new RankBll();
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
            string qq = string.Empty;
            qq = TokenBll.GetQQByUserID(userid);
            if (!string.IsNullOrEmpty(qq))
            {
                //订阅列表
                ViewBag.EmRss = rank_bll.GetEmRssList(qq);
            }
            List<Rank> ranklst = rank_bll.GerRankDayLst();
            string sortFlag = Request["SortFlag"];
            string sortName = Request["SortName"];
            if (!string.IsNullOrEmpty(sortName))
            {
                var query = ranklst.AsQueryable();
                ranklst = query.OrderBy(sortName + " " + sortFlag).ToList();
            }
            ViewBag.RankDayLst = ranklst;
            ViewBag.SortFlag = sortFlag;
            ViewBag.SortName = sortName;
            ViewBag.RankType = type;
            return View();
        }

        /// <summary>
        /// 添加订阅
        /// </summary>
        /// <param name="ruid">被订阅的qq号</param>
        /// <returns></returns>
        public string PostAddEmRss(string ruid)
        {
            string result = "-1";
            try
            {
                int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
                string qq = TokenBll.GetQQByUserID(userid);
                RankBll r_bll = new RankBll();
                result = r_bll.AddEmRss(ruid, qq);
                //获取还能订阅几位高手
                int rsscount = 0;
                EmRss emrss = r_bll.GetEmRssList(qq);
                if (emrss != null)
                {
                    string[] rsslst = emrss.data;
                    if (rsslst != null)
                    {
                        rsscount = 3 - rsslst.Length;
                    }
                }

                //获取订阅高手的昵称
                string nickname = r_bll.GetNickName(ruid);
                result = result + "|" + rsscount + "|" + nickname;

            }
            catch (Exception)
            {
            }
            return result;
        }
        
        /// <summary>
        /// 判断用户是否为参赛用户
        /// </summary>
        /// <returns></returns>
        public int IsCanSaiUser()
        {
            int result = -1;
            try
            {
                int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
                string qq = TokenBll.GetQQByUserID(userid);
                if (!string.IsNullOrEmpty(qq))
                {
                    result = 1;
                }
            }
            catch (Exception ex)
            {
            }
            return result;
        }

        //判断用户是否还可以订阅
        public int IsDingYue()
        {
            int result = -1;
            try
            {
                int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
                string qq = TokenBll.GetQQByUserID(userid);
                RankBll rank_bll = new RankBll();
                EmRss emrss = rank_bll.GetEmRssList(qq);
                if (emrss != null)
                {
                    string[] rsslst = emrss.data;
                    if (rsslst != null)
                    {
                        if (rsslst.Length < 3)
                        {
                            result = 1;
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            return result;
        }


        /// <summary>
        /// 删除订阅
        /// </summary>
        /// <param name="ruid">被订阅的qq号</param>
        /// <returns></returns>
        public string PostDelEmRss(string ruid)
        {
            string result = "-1";
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
            string qq = string.Empty;
            if (userid == 0)
            {
                result = "-1";
            }
            else
            {
                qq = TokenBll.GetQQByUserID(userid);
                if (string.IsNullOrEmpty(qq))
                {
                    //return Content("QQ号码不存在");
                    result = "-1";
                }
                RankBll r_bll = new RankBll();
                result = r_bll.DelEmRss(ruid, qq);
            }
            return result;
        }

        /// <summary>
        /// 我的订阅列表
        /// </summary>
        /// <returns></returns>
        public ActionResult rank_myrss()
        {
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
            string qq = string.Empty;
            if (userid == 0)
            {
                return null;
            }
            else
            {
                qq = TokenBll.GetQQByUserID(userid);
                if (string.IsNullOrEmpty(qq))
                {
                    return Content("-1");
                }
                RankBll rank_bll = new RankBll();
                //订阅列表
                ViewBag.EmRss = rank_bll.GetEmRssList(qq);
            }
            return View();
        }

        /// <summary>
        /// 高手操作
        /// </summary>
        /// <returns></returns>
        public ActionResult rank_gscz()
        {
            int userid = TokenBll.GetUserIDByCookie();  //从Cookie中获取用户编号
            string qq = string.Empty;

            qq = TokenBll.GetQQByUserID(userid);
            RankBll rank_bll = new RankBll();
            MsgBll msgBll = new MsgBll();
            List<Msg> list = msgBll.GetMsg(qq, 0);
            list = list.OrderByDescending(x => x.msgid).ToList();
            ViewBag.RankMsgLst = list;

            return View();
        }
        /// <summary>
        /// 高手揭秘
        /// </summary>
        /// <returns></returns>
        public ActionResult rank_gsjm(string qq)
        {
            //持仓
            TradeBll tradeBll = new TradeBll();
            ViewBag.Stocks = tradeBll.GetStocks(qq);


            return View();
        }
        /// <summary>
        /// 高手揭秘左侧信息
        /// </summary>
        /// <param name="qq"></param>
        /// <returns></returns>
        public ActionResult rank_gsjm_left(string qq)
        {
            TradeBll tradeBll = new TradeBll();
            //根据qq获取用户资产信息
            ViewBag.Assets = tradeBll.GetAssets(qq);

            RankBll rank_bll = new RankBll();
            QQUserInfo qquserinfo = rank_bll.GetQQUserInfo(qq);
            if (qquserinfo != null)
            {
                ViewBag.UserPic = qquserinfo.face;
                ViewBag.NickName = qquserinfo.nickname;
            }
            
            return View();
        }
        /// <summary>
        /// 高手揭秘 交易明细
        /// </summary>
        /// <param name="qq"></param>
        /// <returns></returns>
        public ActionResult rank_gsjm_detail(string qq)
        {
            TradeBll tradeBll = new TradeBll();
            ViewBag.DealOrders = tradeBll.GetDealOrders(qq, "2014-10-01", "2024-10-01");

            return View();
        }
        /// <summary>
        /// 当日高手聚焦
        /// </summary>
        /// <returns></returns>
        public ActionResult rank_gsjj_day()
        {
            FocusBll fbll = new FocusBll();
            DateTime lastDate = DateTime.Now.Date;
            List<TB_FocusStock> lst = fbll.GetFocusStockList(lastDate.AddDays(-1), lastDate.AddDays(1));
            if (lst != null)
            {
                lst = lst.Where(x => x.TradeDate >= lastDate).OrderByDescending(x => x.Number).ThenByDescending(x => x.TolFund).Take(6).ToList();
            }
            ViewBag.gsjj_day_lst = lst;

            return View();
        }
        /// <summary>
        /// 昨日高手聚焦
        /// </summary>
        /// <returns></returns>
        public ActionResult rank_gsjj_yesterday()
        {
            FocusBll fbll = new FocusBll();
            DateTime lastDate = DateTime.Now.Date;
            List<TB_FocusStock> lst = fbll.GetFocusStockList(lastDate.AddDays(-1), lastDate.AddDays(1));
            if (lst != null)
            {
                lst = lst.Where(x => x.TradeDate < lastDate).OrderByDescending(x => x.Number).ThenByDescending(x => x.TolFund).Take(6).ToList();
            }
            ViewBag.gsjj_yesterday = lst;
            return View();
        }
        /// <summary>
        /// 根据qq获取qq昵称
        /// </summary>
        /// <param name="qq"></param>
        /// <returns></returns>
        public string GetNickNameByQQ(string qq)
        {
            RankBll rankbll = new RankBll();
            return rankbll.GetNickName(qq);
        }

        #endregion

        /// <summary>
        /// 接收订阅信息
        /// </summary>
        /// <returns></returns>
        public string receive()
        {
            bool flag = false;

            try
            {
                string from_uin = Request["from_uin"];
                string token = Request["token"];    //7a2d1c34778ed48e8d702754fa572bf1 验证
                string msg = Request["msg"];// StringHelper.UnicodeToStr(Request["msg"]);
                string to_qq = Request["to_qq"];
                string timestamp = Request["timestamp"];

                LogManager.DefaultLogger.Debug(string.Concat("qq receive.from_uin:", from_uin, ".token:", token, ".msg:", msg, ".to_qq:", to_qq, ".timestamp:", timestamp));

                if (!string.IsNullOrEmpty(from_uin) && !string.IsNullOrEmpty(token) && !string.IsNullOrEmpty(msg)
                    && !string.IsNullOrEmpty(to_qq) && !string.IsNullOrEmpty(timestamp))
                {
                    MsgBll msgBll = new MsgBll();

                    msgBll.ReceiveMsg(from_uin, token, msg, to_qq, timestamp);

                    flag = true;
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(ex, "接收订阅信息失败");
            }

            if (flag)
            {
                return "{\"result\": true}";
            }
            else
            {
                return "{\"result\": false}";
            }
        }

        /// <summary>
        /// 获取订阅信息（提供客户端调用）
        /// </summary>
        /// <returns></returns>
        public string msg()
        {
            string token = Request["token"];
            string reqMsgid = Request["msgid"];

            if (string.IsNullOrEmpty(token))
            {
                return string.Empty;
            }
            if (string.IsNullOrEmpty(reqMsgid))
            {
                reqMsgid = "0";
            }

            string ret = string.Empty;

            int userid = TokenBll.GetUserIDByTokenWithWS(token);

            if (userid > 0)
            {
                string qq = TokenBll.GetQQByUserID(userid);

                if (!string.IsNullOrEmpty(qq))
                {
                    long msgid = 0;

                    if (long.TryParse(reqMsgid, out msgid))
                    {
                        MsgBll msgBll = new MsgBll();

                        List<Msg> list = msgBll.GetMsg(qq, msgid);

                        if (list != null && list.Count > 0)
                        {
                            ret = JsonHelper.ToJson(list);
                        }
                    }
                }
            }

            return ret;
        }
    }
}
